public class demo4 {
    public static void main(String[] args) {
        //定义数组
        int[] arr = {1, 2, 3};
        arrToString(arr);
    }

    //遍历数组并且拼接成一个字符串
    public static String arrToString(int[] arr) {
        if (arr == null) {
            return "";
        }

        if (arr.length == 0) {
            return "[]";
        }

        String result = "[";
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                result = result + arr[i];
            }else{
                result = result + arr[i] + ",";
            }

        }

        result = result + "]";
        System.out.println(result);
        return result;
    }
}
